package cn.ekgc.bmdsad.day13;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class S {
    static Scanner input = new Scanner ( System.in );

    public static void main(String[] args) {
        try (ServerSocket ss = new ServerSocket ( 8080 )) {
                System.out.println ( "成功，等待连接" );
                Socket c = ss.accept ();
                System.out.println ("开始聊天");
                new Thread ( () -> {
                    try {
                        BufferedReader in = new BufferedReader ( new InputStreamReader ( c.getInputStream () ) );
                        while (true) {
                            System.out.println ( in.readLine () );
                        }
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                } ).start ();


                PrintWriter out = new PrintWriter ( c.getOutputStream () );
                while (true) {
                String msg = input.nextLine ();
                out.println ( msg );
                out.flush ();
            }


        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}
